package
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	public class CGameState_Title extends CGameState
	{
		private const CURSOR_STATE_STARTGAME	:uint = 1;
		private const CURSOR_STATE_RANKING		:uint = 2;
		private var m_cCursorState :uint = CURSOR_STATE_STARTGAME;
		
		private var m_refUI :CMC_GameState_Title = null;
		
		public function CGameState_Title(refGame :CGame)
		{
			super(refGame);
			
			cMovieClip = new CMC_GameState_Title();
			m_refUI = CMC_GameState_Title(cMovieClip);
			
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_1_TITLE, int.MAX_VALUE);
		}
		override public function EnterState() :void
		{
			cMovieClip.stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
			
			m_refUI.btnStartGame.addEventListener(MouseEvent.CLICK, OnClick_StartGame);
			m_refUI.btnRanking.addEventListener(MouseEvent.CLICK, OnClick_Ranking);
		}
		override public function LeaveState() :void
		{
			m_refUI.btnRanking.removeEventListener(MouseEvent.CLICK, OnClick_Ranking);
			m_refUI.btnStartGame.removeEventListener(MouseEvent.CLICK, OnClick_StartGame);
			
			cMovieClip.stage.removeEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
		}
		
		private function MoveCursor() :void
		{
			switch(m_cCursorState)
			{
			case CURSOR_STATE_STARTGAME:
				m_cCursorState = CURSOR_STATE_RANKING;
				m_refUI.mcCursor.x = 400;
				m_refUI.mcCursor.y = 450;
				break;
			case CURSOR_STATE_RANKING:
				m_cCursorState = CURSOR_STATE_STARTGAME;
				m_refUI.mcCursor.x = 400;
				m_refUI.mcCursor.y = 400;
				break;
			}
			
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_3_MOVECURSOR);
		}
		
		private function GoToExplain() :void
		{
			m_refGame.EnterState(CGameState.GAME_STATE_EXPLAIN);
			
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_7_EAT);
		}
		private function GoToRanking() :void
		{
			m_refGame.EnterState(CGameState.GAME_STATE_RANKING);
			
			CSoundManager.instance.PlaySound(CSoundManager.SOUND_7_EAT);
		}
		
		private function OnClick_StartGame(event :Event) :void
		{
			GoToExplain();
		}
		private function OnClick_Ranking(event :Event) :void
		{
			GoToRanking();
		}
		private function OnKeyDown(event :KeyboardEvent) :void
		{
			if (event.keyCode == Keyboard.ENTER)
			{
				if (m_cCursorState == CURSOR_STATE_STARTGAME)
				{
					GoToExplain();
				}
				else if (m_cCursorState == CURSOR_STATE_RANKING)
				{
					GoToRanking();
				}
			}
			else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
			{
				MoveCursor();
			}
		}
		
	}
}